package com.fighting;

import org.activiti.engine.*;
import org.activiti.engine.impl.persistence.entity.VariableScopeImpl;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

public class ActiDemo {
    private static final String processKey = "commonProcess1";
    private ProcessEngine engine;

    @Before
    public void getEngine() {
        engine = ProcessEngines.getDefaultProcessEngine();
    }

    @Test
    public void testGetEngine() {
        // 方式1：默认方式，配置文件名称必须为：activiti.cfg.xml
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 方式2：配置文件名任意
        ProcessEngine processEngine1 = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg1.xml", "processEngineConfiguration").buildProcessEngine();
        System.out.println(processEngine1);
    }

    @Test
    public void testDeployment() {
        RepositoryService repositoryService = engine.getRepositoryService();
        repositoryService.createDeployment().name("通用流程")
                .addClasspathResource("diagrams/commonProcess1.bpmn")
                .addClasspathResource("diagrams/commonProcess1.png")
                .deploy();
    }

    @Test
    public void testDeploymentZip() throws FileNotFoundException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("diagrams/diagrams.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        RepositoryService repositoryService = engine.getRepositoryService();
        repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy();
    }

    @Test
    public void testStartProcess() {
        RuntimeService runtimeService = engine.getRuntimeService();
        Map<String,Object>map=new HashMap<>();
        map.put("a","b");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey,map);
        System.out.println(((VariableScopeImpl) processInstance).getVariable("a"));
        // 该方式获取不到变量
        Map<String, Object> processVariables = processInstance.getProcessVariables();
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());
        System.out.println(processVariables);
    }


    @Test
    public void testCompleteTask() {
        String username = "u1";
        RuntimeService runtimeService = engine.getRuntimeService();
        TaskService taskService = engine.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey(processKey).taskCandidateOrAssigned(username).singleResult();
        if (task != null) {
            taskService.complete(task.getId());
        }
    }

    @Test
    public void testQueryTask() {
        String username = "u3";
        TaskService taskService = engine.getTaskService();
        taskService.createTaskQuery().processDefinitionKey(processKey)
                .taskCandidateOrAssigned(username).list().forEach(task -> {
            System.out.println(task.getTaskDefinitionKey());
            System.out.println(task.getName());
            System.out.println(task.getId());
        });
        // 当以CandidateUser查询时assignee必须为null
        Task task1 = taskService.createTaskQuery().processDefinitionKey(processKey)
                .taskCandidateUser("u33").singleResult();

        System.out.println(task1);
    }
    // 删除正在执行的实例
    @Test
    public void testDeleteRunningProcess(){
        String prcessInstance="25001";
        RuntimeService runtimeService = engine.getRuntimeService();
        runtimeService.deleteProcessInstance(prcessInstance,"测试删除");
        engine.getHistoryService().deleteHistoricProcessInstance(prcessInstance);
    }
    @Test
    public void testProcess(){
        String prcessInstanceId="25001";
        RuntimeService runtimeService = engine.getRuntimeService();
        runtimeService.suspendProcessInstanceById(prcessInstanceId);
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().suspended().list();
        for(ProcessInstance processInstance:list){
            System.out.println(processInstance);
        }

    }
}
